import './BranchCompany.scss'
import { Button } from 'antd'
import { useState, useRef } from 'react'
import { ProTable } from '@ant-design/pro-components'
import { useMount, useRequest, useSize } from 'ahooks'

import { Http } from '../../../Api'

import { commonTableList, BuiltInData } from '../Common/common'
import { EnumComments } from '../Common/Conversion'
const SumMapping = [
    { uuid: 1, label: '中标金额' },
    { uuid: 4, label: '合同金额' },
    { uuid: 2, label: '总体收入' },
    { uuid: 3, label: '本年收入' },
]

export const BranchCompany = () => {
    const actionRef = useRef()
    const formRef = useRef()
    const TableBox = useRef()
    const size = useSize(TableBox)
    const ClientData = JSON.parse(window.localStorage.getItem('ClientData'))
    const [Active, setActive] = useState('')
    const [BranchCompany, setBranchCompany] = useState([])
    const [Columns, setColumns] = useState()
    const [SummaryList, setSummaryList] = useState({ 中标金额: '0.00', 总体收入: '0.00', 本年收入: '0.00' })
    const [IndexNumber, setIndexNumber] = useState(1)
    const [SizeNumber, setSizeNumber] = useState(10)

    const { runAsync: getBranchCompany } = useRequest(Http, {
        manual: true,
        onSuccess: res => {
            setActive(res.list[0].uuid)
            setBranchCompany(res.list)
        },
    })

    const { runAsync: getData } = useRequest(Http, { manual: true })
    const { runAsync: getExcel } = useRequest(Http, { manual: true })

    const columns = async () => [
        commonTableList.uuid,
        { ...commonTableList.tableName, width: 320, ellipsis: false },
        { ...commonTableList.fullName, width: 320, ellipsis: false },
        {
            dataIndex: 'projectManagementTeam',
            title: '项目经理部',
            valueType: 'input',
            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'projectManager',
            title: '项目经理',
            valueType: 'input',
            width: 200,
        },
        {
            dataIndex: 'owner',
            title: '业主名称',
            valueType: 'input',
            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'acceptanceAmount',
            title: '中标金额',
            valueType: 'input',

            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: '总体收入',
            title: '总体收入',
            valueType: 'input',
            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: '本年收入',
            title: '本年收入',
            valueType: 'input',
            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'constructionSite',
            title: '施工地点',
            valueType: 'input',

            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'numberLength',
            title: '施工桩号及长度',
            valueType: 'input',

            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'qualifiedUnit',
            title: '资质单位',
            valueType: 'input',

            width: 200,
        },
        {
            dataIndex: 'cooperationType',
            title: '项目性质',
            valueType: 'select',

            width: 100,
            valueEnum: await EnumComments('cooperationType'),
        },
        {
            dataIndex: 'engineeringScale',
            title: '工程规模',
            valueType: 'select',

            hideInSearch: true,
            width: 100,
            valueEnum: await EnumComments('engineeringScale'),
        },
        {
            dataIndex: 'winningBidder',
            title: '中标单位',
            valueType: 'input',

            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'construction',
            title: '施工单位',
            valueType: 'input',

            width: 200,
        },
        {
            dataIndex: 'contractStartDate',
            title: '合同开始时间',
            valueType: 'Date',

            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'contractEndDate',
            title: '合同结束时间',
            valueType: 'Date',

            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'workStartDate',
            title: '开工日期',
            valueType: 'Date',

            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'acceptanceDate',
            title: '中标日期',
            valueType: 'Date',

            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'workEndDate',
            title: '竣工日期',
            valueType: 'Date',

            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'contractAmount',
            title: '合同金额',
            valueType: 'input',

            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'provisionalAmount',
            title: '暂定金金额',
            valueType: 'input',

            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'chaptersAmount',
            title: '百章合计金额',
            valueType: 'input',

            hideInSearch: true,
            width: 200,
        },
        {
            dataIndex: 'isInsideEnterprise',
            title: '是否为工建内',
            valueType: 'select',

            width: 200,
            valueEnum: BuiltInData.isYes,
            rules: true,
            hideInSearch: true,
        },
        {
            dataIndex: 'isTendering',
            title: '是否招标',
            valueType: 'select',

            hideInSearch: true,
            width: 200,
            valueEnum: BuiltInData.isYes,
        },
        {
            dataIndex: 'engineeringStatus',
            title: '工程状态',
            valueType: 'select',

            width: 100,
            valueEnum: await EnumComments('engineeringStatus'),
        },
        {
            dataIndex: 'isContractSigned',
            title: '是否签合同',
            valueType: 'select',

            hideInSearch: true,
            width: 200,
            valueEnum: BuiltInData.isYes,
        },
        {
            dataIndex: 'embodyType',
            title: '收录类型',
            valueType: 'select',

            width: 200,
            valueEnum: await EnumComments('embodyType'),
            rules: true,
        },
        {
            dataIndex: 'qualificationType',
            title: '中标资质类型',
            valueType: 'select',

            hideInSearch: true,
            width: 200,
            valueEnum: await EnumComments('qualificationType'),
            rules: true,
        },
        {
            dataIndex: 'address',
            title: '地市',
            valueType: 'select',
            valueEnum: ClientData.dictionary['ff17b144-6a23-4b0d-84a5-9aab9f52225b'],

            width: 200,
        },
        {
            dataIndex: 'engineeringType',
            title: '工程类型',
            valueType: 'select',
            valueEnum: ClientData.dictionary['3b863e8d-4476-44e2-b487-96d0a948a080'],

            width: 200,
        },
        {
            dataIndex: 'isEnabled',
            title: '工地是否启用',
            valueType: 'select',

            width: 200,
            valueEnum: BuiltInData.IsEnabled,
        },
        {
            dataIndex: 'isEnabledCost',
            title: '成本表中是否启用',
            valueType: 'select',

            width: 200,
            valueEnum: BuiltInData.IsEnabled,
        },
        {
            dataIndex: 'isEnabledFinance',
            title: '财务表中是否启用',
            valueType: 'select',

            width: 200,
            valueEnum: BuiltInData.IsEnabled,
        },
        { ...commonTableList.sortId, rules: true },
        commonTableList.remark,

        { ...commonTableList.BeginDate, title: '中标金额开始', dataIndex: 'BeginAmount' },
        { ...commonTableList.EndDate, title: '中标金额结束', dataIndex: 'EndAmount' },
        { ...commonTableList.BeginDate, title: '合同开始-从' },
        { ...commonTableList.EndDate, title: '到-合同开始' },
        { ...commonTableList.BeginDate, title: '合同结束-从', dataIndex: 'BeginStatDate' },
        { ...commonTableList.EndDate, title: '到-合同结束', dataIndex: 'EndStatDate' },
        { ...commonTableList.BeginDate, title: '总体收入开始', dataIndex: 'Begin1Amount' },
        { ...commonTableList.EndDate, title: '总体收入结束', dataIndex: 'End1Amount' },
        { ...commonTableList.BeginDate, title: '本年收入开始', dataIndex: 'Begin2Amount' },
        { ...commonTableList.EndDate, title: '本年收入结束', dataIndex: 'End2Amount' },
    ]

    const onActive = e => {
        setActive(e)
        actionRef.current.reload()
    }

    useMount(async () => {
        getBranchCompany({ url: 'getBranchCompany' })
        setColumns(await columns())
    })

    const getRequest = async e => {
        const { current, ...item } = e
        const data = { pageIndex: current, ...item, parentOrganizationUuid: Active }
        return getData({ url: 'getEngineeringOrganizationListByCompanyUuid', data }).then(res => {
            setSummaryList(res.summaryList)
            return { data: res.list, total: res.total, success: true }
        })
    }

    const onExcel = () => {
        const data = { parentOrganizationUuid: Active, ...formRef.current.getFieldsValue() }
        getExcel({ url: 'getEngineeringOrganizationsExcel', data })
    }

    return (
        <div className="BranchCompany">
            <div className="Left">
                {BranchCompany.map(({ uuid, tableName, organizationCount }) => (
                    <div key={uuid} className={Active === uuid ? 'active' : ''} onClick={() => onActive(uuid)}>
                        <div>{tableName}</div>
                        <div>{organizationCount}</div>
                    </div>
                ))}
            </div>
            <div className="Right" ref={TableBox}>
                {size && Columns && (
                    <ProTable
                        rowKey="uuid"
                        formRef={formRef}
                        actionRef={actionRef}
                        columns={Columns}
                        request={e => getRequest(e)}
                        pagination={{
                            pageSize: SizeNumber,
                            showSizeChanger: true,
                            onChange: (e, a) => {
                                setIndexNumber(e)
                                setSizeNumber(a)
                            },
                        }}
                        headerTitle={
                            <div className="headerTitle">
                                {SumMapping.map(({ uuid, label }) => (
                                    <div key={uuid}>
                                        <span>{label} : </span>
                                        <span>{SummaryList[label]}</span>
                                        <span>万元</span>
                                    </div>
                                ))}
                            </div>
                        }
                        toolBarRender={() => [
                            <Button key="button" type="primary" onClick={onExcel}>
                                导出Excel
                            </Button>,
                        ]}
                        scroll={(() => {
                            let { width: x, height } = size
                            let y = height - 265
                            return { x, y }
                        })()}
                    />
                )}
            </div>
        </div>
    )
}
